from django.http import HttpResponse
from django.contrib.auth import authenticate
from django.contrib import auth
# from django.contrib.auth.decorators import login_required
import json

from apps.ganji.models import Citys
from apps.ganji.models import Hezu
from apps.ganji.models import Ershoufang
from apps.ganji.models import Zufang

from apps.comm.login_required import login_required
from apps.comm import define_info
# Create your views here.


# @login_required
def get_citys_data(request):
    print('get citys data')

    city_list = [city_info.area for city_info in Citys.objects.all()]

    print(city_list)
    _info = define_info.res_info
    _info['msg'] = 'citys list'
    _info['result'] = city_list

    res = HttpResponse()
    res.content = json.dumps(_info)

    return res


def get_hezu_count(request):
    print('get hezu info')
    city_id_list = [id[0] for id in Citys.objects.values_list('id')]
    print(city_id_list)

    city_name_list = list()
    hezu_count_list = list()
    for city_id in city_id_list:
        obj = Citys.objects.get(id=city_id)
        city_name = obj.area
        city_name_list.append(city_name)
        hezu_count = obj.hezu_set.count()
        hezu_count_list.append(hezu_count)

    _info = define_info.res_info
    _info['msg'] = 'hezu_count'
    _info['result'] = dict(
        city_name_list=city_name_list,
        hezu_count_list=hezu_count_list
    )

    res = HttpResponse(json.dumps(_info))
    return res


def get_zufang_count(request):
    print('get zufang count')
    city_id_list = [id[0] for id in Citys.objects.values_list('id')]
    city_name_list = list()
    zufang_count_list = list()
    for city_id in city_id_list:
        obj = Citys.objects.get(id=city_id)
        city_name = obj.area
        city_name_list.append(city_name)
        zufang_count = obj.zufang_set.count()
        zufang_count_list.append(zufang_count)

    _info = define_info.res_info
    _info['msg'] = 'hezu_count'
    _info['result'] = dict(
        city_name_list=city_name_list,
        zufang_count_list=zufang_count_list
    )

    res = HttpResponse(json.dumps(_info))
    return res


def get_ershoufang_count(request):
    print('get ershoufang count')
    city_id_list = [id[0] for id in Citys.objects.values_list('id')]
    city_name_list = list()
    ershoufang_count_list = list()
    for city_id in city_id_list:
        obj = Citys.objects.get(id=city_id)
        city_name = obj.area
        city_name_list.append(city_name)
        ershoufang_count = obj.ershoufang_set.count()
        ershoufang_count_list.append(ershoufang_count)

    _info = dict()
    _info['msg'] = 'hezu_count'
    _info['result'] = dict(
        city_name_list=city_name_list,
        ershoufang_count_list=ershoufang_count_list
    )

    res = HttpResponse(json.dumps(_info))
    return res


def get_pie_for_city(request):
    _info = dict()
    city_id = request.GET.get('city_id')
    if not city_id:
        city_id = 1

    obj = Citys.objects.get(id=city_id)
    city_name = obj.area

    # hezu_object = obj.hezu_set.all()
    hezu_address1 = obj.hezu_set.values_list('address1')
    print(hezu_address1)
    a = list()
    for address1 in hezu_address1:
        a.append(address1[0])

    _info['result'] = a
    _info['city_name'] = city_name

    res = HttpResponse(json.dumps(_info))
    return res
